/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.ams.controller;

import cn.tedu.mall.ams.service.IRoleService;
import cn.tedu.mall.common.pojo.admin.dto.RoleAddDTO;
import cn.tedu.mall.common.pojo.admin.dto.RoleQueryDTO;
import cn.tedu.mall.common.pojo.admin.dto.RoleUpdateDTO;
import cn.tedu.mall.common.pojo.admin.vo.RoleVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/ams/role")
@Api(tags = "角色模块")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
     * 角色列表
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "后台查看角色列表")
    @GetMapping("")
    @ApiImplicitParams({
        @ApiImplicitParam(
            value = "页数",
            name = "pageNum",
            required = true),
        @ApiImplicitParam(
            value = "条数",
            name = "sizeNum",
            required = true)
    })
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<RoleVO>> listRoles(Integer pageNum, Integer sizeNum) {
        JsonPage<RoleVO> roles = roleService.listRoles(pageNum, sizeNum);
        return JsonResult.ok(roles);
    }

    /**
     * 根据角色名称模糊查询角色
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "根据角色名称模糊查询角色列表")
    @GetMapping("/query")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "页数", name = "pageNum", required = true),
        @ApiImplicitParam(value = "条数", name = "sizeNum", required = true),
        @ApiImplicitParam(value = "角色名称", name = "query", required = true)
    })
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<RoleVO>> queryRoles(
        Integer pageNum,
        Integer sizeNum,
        String query
    ) {
        JsonPage<RoleVO> roles = roleService.queryRoles(pageNum, sizeNum, query);
        return JsonResult.ok(roles);
    }

    /**
     * 新增角色
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @CacheEvict(cacheNames = "role", allEntries = true)
    public JsonResult addRole(RoleAddDTO roleAddDTO) {
        roleService.addRole(roleAddDTO);
        return JsonResult.ok();
    }

    /**
     * 更新角色
     */
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "更新角色")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @CacheEvict(cacheNames = "role", allEntries = true)
    public JsonResult updateRole(RoleUpdateDTO roleUpdateDTO) {
        roleService.updateRole(roleUpdateDTO);
        return JsonResult.ok();
    }

    /**
     * 删除角色
     */
    @ApiOperationSupport(order = 50)
    @ApiOperation(value = "删除角色", notes = "只能删除无关联角色")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @CacheEvict(cacheNames = "role", allEntries = true)
    public JsonResult deleteRole(Long id) {
        roleService.deleteRole(id);
        return JsonResult.ok();
    }

    /**
     * 角色列表完整查询
     */
    @ApiOperationSupport(order = 60)
    @ApiOperation("角色列表完整查询")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @Cacheable(cacheNames = "role")
    public JsonResult<JsonPage<RoleVO>> queryRoleList(RoleQueryDTO roleQueryDTO) {
        JsonPage<RoleVO> roleList = roleService.queryRoleList(roleQueryDTO);
        return JsonResult.ok(roleList);
    }

    /**
     * 启动停用角色
     */
    @ApiOperationSupport(order = 70)
    @ApiOperation("启用停用角色")
    @PostMapping("/enable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @CacheEvict(cacheNames = "role", allEntries = false)
    public JsonResult enableRole(
        Long id, Integer
        enable
    ) {
        roleService.enableRole(id, enable);
        return JsonResult.ok();
    }

}
